package website.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import website.advice.BaseResponse;
import website.domain.ResModel;
import website.domain.User;
import website.service.UserService;

import java.util.List;

@Api("用户控制类")
@BaseResponse
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("注册")
    @PostMapping(path = "/register",produces = "application/json")
    public ResModel register(@RequestBody User user){
        return userService.register(user);
    }

    @ApiOperation("登录")
    @PostMapping(path = "/login",produces = "application/json")
    public ResModel login(@RequestBody User user){
        return userService.login(user);
    }

    @ApiOperation("忘记密码")
    @PostMapping(path = "/forget",produces = "application/json")
    public ResModel forget(@RequestBody User user){
        return userService.forget(user);
    }

    @ApiOperation("获取邮箱验证码")
    @PostMapping(path = "/code",produces = "application/json")
    public ResModel getCode(@RequestBody User user){
        return userService.getCode(user);
    }

    @ApiOperation("修改用户信息")
    @PutMapping(path = "",produces = "application/json")
    public ResModel updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }

    @ApiOperation("上传头像")
    @PostMapping(path = "/avatar",produces = "application/json")
    public ResModel upload(MultipartFile file){
        return userService.upload(file);
    }

    @ApiOperation("查询我的信息")
    @GetMapping("/profile")
    public ResModel<User> getMyInfo(){
        return userService.getMyInfo();
    }

    @ApiOperation("查询用户信息")
    @GetMapping("/{uid}")
    public ResModel<User> getUserInfo(@PathVariable Integer uid){
        return userService.getUserInfo(uid);
    }

    @ApiOperation("分页查询用户信息")
    @GetMapping("/{current}/{size}")
    public ResModel<IPage<User>> list(@PathVariable Integer current, @PathVariable Integer size, User user){
        return userService.getByPage(current,size,user);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{uid}")
    public ResModel delUser(@PathVariable Integer uid){
        return userService.delUser(uid);
    }

    @ApiOperation("统计用户数量")
    @GetMapping("/count")
    public ResModel<Integer> getUserCount(){
        return userService.getUserCount();
    }

    @ApiOperation("用户排行榜")
    @GetMapping("/ranking")
    public ResModel<List<User>> getUserRank(){
        return userService.getUserRank();
    }
}
